<?php
/**
 * Folder Model
 *
 * @author      Hyper <hyper@flashbay.com>
 * @copyright   Copyright (c) 2011 (http://www.saleshero.com)
 */
class Application_Model_Folder extends SalesHero_Model_Abstract
{

    /**
     * All the folders info
     *
     * @var array
     */
    private $_folders = array();

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Get folder info
     *
     * @param  int $id
     * @return array
     */
    public function getInfo($id)
    {
        $select = $this->db->select()->from('file_folder')->where('folder_id = ?', $id);
        return $this->db->fetchRow($select);
    }

    /**
     * Get all the folders
     *
     * @return array
     */
    public function getFolders()
    {
        if (!empty($this->_folders)) {
            return $this->_folders;
        } else {
            $select = $this->db->select()->from('file_folder')->order('folder_name');
            return $this->_folders = $this->db->fetchAll($select);
        }
    }

    /**
     * Get select menu options
     *
     * @param int $id
     * @param int $parent
     * @param int $depth
     * @return string
     */
    public function menuOption($id = 0, $parent = 0, $depth = 0)
    {
        $data = $this->getFolders();

        $space = '';
        for ($i = 0; $i < $depth; $i++) {
            $space .= '&nbsp;&nbsp;&nbsp;&nbsp;';
        }

        $childs = array();
        foreach ($data as $item) {
            if ($item['parent_id'] == $parent) {
                $childs[] = $item;
            }
        }

        $list = '';
        if ($childs) {
            foreach ($childs as $child) {
                ($id == $child['folder_id']) ? $sel = 'selected' : $sel = '';
                $list .= "<option value='{$child['folder_id']}' {$sel}>{$space}{$child['folder_name']}</option>";
                $list .= $this->menuOption($id, $child['folder_id'], ++$depth);
            }
        }
        return $list;
    }

    /**
     * Add a new folder
     *
     * @param  array    $params
     * @return int      The folder ID
     */
    public function add($params)
    {
        $data = array(
            'parent_id' => $params['parent_id'],
            'folder_name' => trim($params['folder_name']),
            'description' => trim($params['description'])
        );
        $validator = new Zend_Validate_StringLength(array(
            'min' => 1,
            'max' => 50,
        ));
        if (!$validator->isValid($data['folder_name'])) {
            throw new Exception(current($messages));
        }
        $this->db->insert('file_folder', $data);
        return $this->db->lastInsertId();
    }

    /**
     * Update folder info
     *
     * @param array $params
     */
    public function update($params)
    {
        $data = array(
            'parent_id' => $params['parent_id'],
            'folder_name' => trim($params['folder_name']),
            'description' => trim($params['description'])
        );
        $validator = new Zend_Validate_StringLength(array(
            'min' => 1,
            'max' => 50,
        ));
        if (!$validator->isValid($data['folder_name'])) {
            throw new Exception(current($messages));
        }
        if ($params['id'] <= 0) {
            throw new Exception('You should specify a folder');
        }
        if ($params['id'] == $params['parent_id']) {
            throw new Exception("The folder can't be its parent");
        }
        $where = $this->db->quoteInto('folder_id = ?', $params['id']);
        $this->db->update('file_folder', $data, $where);
    }

    /**
     * Delete a folder
     *
     * @param int $id
     */
    public function delete($id)
    {
        if ($id <= 0) {
            throw new Exception('You should specify a folder');
        }

        $info = $this->getInfo($id);
        if (empty($info)) {
            throw new Exception('The folder dose not exist');
        }

        $where = $this->db->quoteInto('folder_id = ?', $id);
        $this->db->update('file', array('folder_id' => $info['parent_id']), $where);
        $this->db->delete('file_folder', $where);
    }

    /**
     * Transfer files
     *
     * @param int $from
     * @param int $to
     */
    public function transfer($from, $to)
    {
        $where = $this->db->quoteInto('folder_id = ?', $from);
        $this->db->update('file', array('folder_id' => $to), $where);
    }
}